home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 10 Scripting / 01 Berger / scc-lexer.l < prev    next >
Encoding:
Lex Description  |  2001-10-16  |  2.4 KB  |  69 lines

  1. %{
  2. // This file contains all the rules that GNU Flex needs to identify tokens in
  3. // the script source code.  GNU Flex is a lexical analyzer (or lexer) that
  4. // finds tokens in the input stream.  Flex is a tool that is commonly used in
  5. // conjunction with GNU Bison and more information about this tool can be
  6. // found at:   http://www.gnu.org/software/flex/
  7.  
  8. #include "SCC.H"
  9. #include "PTNode.H"
  10. #include "scc-parser.h"
  11.  
  12. // NOTE: The C-style comments below are intentional.  Flex does not like
  13. // C++-style comments inside of its rules definitions below.  Also, you want
  14. // to make sure that the comment does not start in the first column.  Flex
  15. // does not have any way to know distinguish between the /* comment and the
  16. // regular expression /*
  17.  
  18. %}
  19.  
  20.  
  21.   /* Define the regular expressions for comments, whitespace, and numbers. */
  22. comment     \/\/.*$
  23. ws          [\r\n\t ]+
  24. number      [0-9]+
  25.  
  26.  /* This option tells Flex that this script is never ran interactively.  This
  27.   * is needed for Windows builds since a few UNIX functions are not
  28.   * implemented by Microsoft. */
  29. %option never-interactive
  30.  
  31.  
  32. %%
  33.  
  34.  
  35. {comment}   /* comments are ignored */
  36. {ws}        /* whitespace is ignored */
  37.  
  38. {number}    {
  39.               /* This rule matches any integer found in the input stream.  The
  40.                * matched text will be in the yytext string, and we need to
  41.                * convert it to an integer.  After this is done, create a new
  42.                * constant parse tree node and return this node to the parser.
  43.                * This is done through the Bison-defined variable 'yylval'. */
  44.  
  45.               int value = strtol( yytext, NULL, 0 );
  46.               yylval = new ConstantNode( value );
  47.  
  48.               return NUMBER;
  49.             }
  50.  
  51.  /* This regular expression returns any character that was not matched by a
  52.   * rule above to the parser.  This allows the parser to understand the single
  53.   * letter tokens such as + and - */
  54. .           return yytext[0];
  55.  
  56.  
  57. %%
  58.  
  59.  
  60. // Flex requires that this function is defined.  It will call this function
  61. // whenever it reaches the end of the input stream.  If this function returns
  62. // 1, then Flex will terminate reading.  This function is commonly used to
  63. // switch to another file when the current one is finished (for example, the
  64. // user entered two scripts on the command line).
  65. int yywrap()
  66. {
  67.   return 1;
  68. }
  69.